package com.zy.config;

import com.zy.utils.LoginInterceptor;
import com.zy.utils.RefreshTokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * [配置类,注册web层相关配件]
 *
 * @author : [张宇]
 * @version : [v1.0]
 * @createTime : [2024/12/12 20:14]
 */
@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    /*@Autowired
    private LoginInterceptor loginInterceptor;*/

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 注册自定义拦截器
     *控制拦截器执行顺序,RefreshTokenInterceptor优先执行,order 越小越先执行
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        // 登录拦截器,LoginInterceptor和RefreshTokenInterceptor没有加Component注解,所以需要手动注入
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns("/user/code",
                        "/user/login",
                        "/blog/hot",
                        "/shop/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/voucher/**").order(1);
        // token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(redisTemplate)).addPathPatterns("/**").order(0);

    }
}
